<?php
/**
 *@file apppush.php
 *@author Zhe.Zhou(zhouzhe@haibao.com)
 *@date 2015-8-6 下午8:30:12
 *@brief  
 */
namespace haibao\app\data;

class AppPush extends BaseMysql{
	public function __construct(){
		parent::__construct('\haibao\app\model\data\AppPush');
	}
	
	
	public function getPowerList(){
		$sql = "select Id,Version,CreateTime,Status from AppPower order by CreateTime desc";
		$result = $this->query($sql);
		$arr = array();
		$tag = 0;
		while($result->fetch_assoc()){
			$arr[$tag]['Version'] = $result->getData('Version');
			$arr[$tag]['Id'] = $result->getData('Id');
			$arr[$tag]['Status'] = $result->getData('Status');
			$arr[$tag]['CreateTime'] = $result->getData('CreateTime');
			$tag++;
		}
		
		return $arr;
	}
	
	public function changeVersion($id,$status){
		$sql = "update AppPower set Status=%s where Id=%s";
		$this->query($sql,array($status,$id));
		$this->delClosePowerList();
	}
	
	public function setAppClickNum($id){
		$sql = "update AppPush set ClickNum=ClickNum+1 where Id=%s";
		$this->query($sql,array($id));
	}
	
	public function addAppPower($arrSet){
		$sql = "insert into AppPower (Version,CreateBy,CreateTime) values(%s,%s,%s)";
		$this->query($sql,$arrSet);
		$this->delClosePowerList();
	}
	
	public function getClosePoswerList(){
		$cache = new \haibao\app\cache\AppPower();
		$pushList = $cache->getListCache();
		if(empty($pushList)){
			$pushList = $this->setClosePowerList();
		}
		
		return $pushList;
	}
	
	private function delClosePowerList(){
		$cache = new \haibao\app\cache\AppPower();
		$cache->delListCache();
	}
	
	private function setClosePowerList(){
		$pushList = array();
		$cache = new \haibao\app\cache\AppPower();
		$sql = "select Version from AppPower where Status=0";
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			$pushList[] = $result->getData('Version');
		}
		$cache->setListCache($pushList);
		
		return $pushList;
	}
	
	public function getPowerByVersion($version){
	    $sql = 'select * from AppPower where Version=%s';
	    $result = $this->query($sql, array(
	        $version
	    ));
	    
	    return $result ? $result->fetch_assoc() : array();
	}
}